package com.hskn.hss.module.materialDetail.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hskn.hss.entity.AjaxResult;
import com.hskn.hss.exception.HssException;
import com.hskn.hss.module.materialDetail.entity.TlMaterialDetail;
import com.hskn.hss.module.materialDetail.mapper.TlMaterialDetailMapper;
import com.hskn.hss.module.materialDetail.service.ITlMaterialDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author hskn
 * @since 2021-05-08
 */
@Service
public class TlMaterialDetailServiceImpl extends ServiceImpl<TlMaterialDetailMapper, TlMaterialDetail> implements ITlMaterialDetailService {
    @Autowired
    TlMaterialDetailMapper tlMaterialDetailMapper;

    @Override
    public AjaxResult insert(JSONObject params) {
        TlMaterialDetail tlMaterialDetail = params.getObject("data", TlMaterialDetail.class);
        int i = tlMaterialDetailMapper.insert(tlMaterialDetail);
        boolean flag = i > 0 ? true : false;
        if (flag) {
            return AjaxResult.success("操作成功");
        } else {
            return AjaxResult.fail("操作失败，请稍后再试");
        }
    }

    @Override
    public AjaxResult update(JSONObject params) {
        TlMaterialDetail tlMaterialDetail = params.getObject("data", TlMaterialDetail.class);
        int i = tlMaterialDetailMapper.updateById(tlMaterialDetail);
        boolean flag = i > 0 ? true : false;
        if (flag) {
            return AjaxResult.success("操作成功");
        } else {
            return AjaxResult.fail("操作失败，请稍后再试");
        }
    }

    @Override
    public AjaxResult deleteByid(String id) {
        try {
            tlMaterialDetailMapper.deleteById(id);
            return AjaxResult.success("操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败，请稍后再试");
        }
    }

    @Override
    public List<TlMaterialDetail> getitemListBySerialnumber(String Serialnumber) throws HssException {
        QueryWrapper<TlMaterialDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(TlMaterialDetail::getSerialnumber, Serialnumber);
        List<TlMaterialDetail> tlMaterialDetails = tlMaterialDetailMapper.selectList(queryWrapper);
        return tlMaterialDetails;
    }


    @Override
    public void deleteByMap(String maintbid) throws HssException {
        Map map = new HashMap();
        map.put("materialid", maintbid);
        tlMaterialDetailMapper.deleteByMap(map);
    }
}
